﻿<?php


function daddslashes($string, $force = 0) {
	! defined ( 'MAGIC_QUOTES_GPC' ) && define ( 'MAGIC_QUOTES_GPC', get_magic_quotes_gpc () );
	if (! MAGIC_QUOTES_GPC || $force) {
		if (is_array ( $string )) {
			foreach ( $string as $key => $val ) {
				$string [$key] = daddslashes ( $val, $force );
			}
		} else {
			$string = addslashes ( $string );
		}
	}
	return $string;
}

function login($username, $sname) {
	$username = daddslashes ( $username );
	$sname = daddslashes ( $sname );
	if($username == "" || $sname == "" ||!$username)
	return false;
	$query = "SELECT * FROM 2014list WHERE id=\"{$username}\"";
	$result = mysql_query ( $query );
	$row = mysql_fetch_assoc ($result);
	if ($sname == $row['name']) {
		$query = "UPDATE `2014list` SET `last_login` = CURRENT_TIMESTAMP, `last_login_ip` = \"{$_SERVER["REMOTE_ADDR"]}\" WHERE `id` = \"{[$username]}\"";
		mysql_query ( $query );
		return true;
	} else
		return false;
}



function getDetail(&$choice) {
	$query = "SELECT * FROM `votes` WHERE 1";
	$result = mysql_query ( $query );
	while ( $row = mysql_fetch_assoc ($result) ) {
		$choice [] = $row;
	}
}

function getPolled($name) {                                                           //there is a problem here.
	$query = "SELECT `id` FROM `voter` WHERE `stuid` = \"{$username}\"";
	//$query = "SELECT `voteitem` FROM `voter` WHERE `name` = \"{$name}\"";
	$result = mysql_query ( $query );
	$returnRow = array ();
	while ( $row = mysql_fetch_assoc ( $result ) ) {
		$returnRow [] = $row ['id'];
	}
	$returnArray = array ();
	for($i = 1; $i <= 20; $i ++)
		$returnArray [$i] = 0;
	foreach ( $returnRow as $value ) {
		$returnArray [$value] = 1;
	}
	return $returnArray;
}

function getTotalPolled($name) {
	$query = "SELECT COUNT(*) FROM `voter` WHERE `stuid`=\"{$username}\"";
	$result = mysql_query ( $query );
	$row = mysql_fetch_array ($result);
	return $row [0];
}

function updatePoll($id, $name) {
	$query = "SELECT count(*) FROM `voter` WHERE `stuid`=\"{$username}\"";
	$result = mysql_query ( $query );
	$num = mysql_fetch_array ($result);
	if ($num [0] < 10) {
		$query = "SELECT count(*) FROM `voter` WHERE `stuid`=\"{$username}\" AND `id` = \"{$id}\"";
		$result = mysql_query ( $query );
		$num = mysql_fetch_array ($result);
		if ($num [0] == 0) {
			$query = "UPDATE `votes` SET `vote` = `vote` + 1 WHERE `_id` = \"{$id}\"";
			mysql_query ( $query );
			$query = "INSERT INTO `voter` (`stuid`, `id`) VALUES(\"{$username}\", \"{$id}\")";
			mysql_query ( $query );
			return "Success";
		} else
			return "Same poll";
	} else
		return "Max times";

}

function getMajor($collage, &$major) {
	$query = "SELECT * FROM `waws_majorcourses` WHERE `collage` = \"{$collage}\"";
	$result = mysql_query( $query );
	while ( $row = mysql_fetch_assoc ($result) ) {
		$major [] = $row;
	}
}


?>
